<template>
  <div class="system-container">
    <vab-query-form>
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item class="pr8">
          <el-date-picker
            v-model="queryForm.created_at"
            end-placeholder="出库结束日期"
            range-separator=" ~ "
            start-placeholder="出库开始日期"
            type="daterange"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
          <el-button icon="el-icon-plus" type="success" @click="handleEdit">
            添加
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <el-table v-loading="listLoading" border :data="list">
      <el-table-column align="center" label="编号" type="index" width="80" />
      <el-table-column
        align="center"
        label="血袋号"
        prop="blood_id"
        width="100"
      />
      <el-table-column
        align="center"
        label="血型"
        prop="blood.abo"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        label="Rh(D)"
        prop="blood.rhd"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        label="血量(U)"
        prop="blood.capacity"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        label="出库时间"
        prop="created_at"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        label="出库人"
        prop="created_user"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        label="出库原因"
        prop="target"
        show-overflow-tooltip
        width="300"
      >
        <template #default="{ row }">
          {{ getBloodOutboundReason(row) }}
        </template>
      </el-table-column>
      <el-table-column v-if="false" align="center" label="操作" width="140">
        <template #default="{ row }">
          <el-link
            class="mr12"
            icon="el-icon-document"
            size="mini"
            type="primary"
            @click="handleEdit(row)"
          >
            详情
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :pagesize="queryForm.pagesize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @submit="submit" />
  </div>
</template>

<script>
  import { getAll as getList, deleteOne, addOne } from '@/api/bloodOutbound'
  import Edit from './components/edit'
  import { removeEmptyField } from '@/utils'
  import { mapGetters } from 'vuex'
  export default {
    name: 'BloodStockOutbound',
    components: {
      Edit,
    },
    props: {
      activeName: {
        type: String,
        default() {
          return 'fourth'
        },
      },
    },
    data() {
      return {
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        listLoading: false,
        selectRows: '',
        queryForm: {
          created_at: '',
          page: 1,
          pagesize: 10,
        },
        dialogVisible: false,
        bloodMatch: null,
        title: '',
      }
    },
    computed: {
      ...mapGetters(['bloodOutboundReasons']),
      bloodOutboundReasonsObj() {
        const res = {}
        this.bloodOutboundReasons.forEach((item) => {
          res[item.id] = item.name
          if (item.children && item.children.length > 0) {
            item.children.forEach((citem) => {
              res[citem.id] = citem.name
            })
          }
        })
        return res
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleEdit() {
        this.$refs['edit'].show()
      },
      handleDelete(row) {
        this.$confirm('确定删除此条记录吗？', '操作确认', {
          type: 'warning',
        }).then(async () => {
          const { msg } = await deleteOne(row.id)
          this.$baseMessage(msg, 'success', 'vab-hey-message-success')
          this.queryData()
        })
      },
      queryData() {
        delete this.queryForm.page
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(removeEmptyField(this.queryForm))
        this.list = data.data
        this.total = data.total
        this.listLoading = false
      },
      handleRefresh() {
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pagesize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      async submit(postData) {
        const { data, msg } = await addOne(postData)
        this.$baseMessage(msg, 'success', 'vab-hey-message-success')
        console.log(data)
        this.fetchData()
      },
      getBloodOutboundReason(row) {
        const obj = this.bloodOutboundReasonsObj
        const ids = row.blood_outbound_reason_ids
        if (ids.length === 1) {
          return '发血'
        } else if (ids.length === 2) {
          const res = [obj[ids[0]], obj[ids[1]]]
          if (row.descri) {
            res.push(row.descri)
          }
          return res.join(' - ')
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  .pr8 {
    padding-right: 8px;
  }
</style>
